package com.tj.shopping.web.controller;

import com.tj.shopping.entity.Address;
import com.tj.shopping.service.IAddressService;
import com.tj.shopping.service.impl.AddressServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @program: IntelliJ IDEA
 * @description:
 * @author: H
 * @create: 2024-01-06 12:43
 */

@WebServlet("/address")
public class AddressController extends BaseController{
private IAddressService service=new AddressServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        String op = req.getParameter("op");
        if ("add".equals(op)) {
            doAdd(req, resp);
        } else if ("delete".equals(op)) {
            doDeleteAdress(req, resp);
        } else if ("finds".equals(op)) {
            doFindAll(req, resp);
        }  else {
            resp.getWriter().print("no such Method....");
        }


    }


    private void doAdd(HttpServletRequest req, HttpServletResponse resp) throws IOException {
      Address address =parseRequest(req,Address.class);
        System.out.println(address);
        int result= service.add(address);
        printToJson(resp, result);//响应给页面的是m

    }

    private void doDeleteAdress(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int result= service.remove(parseRequest(req));
        printToJson(resp, result);//响应给页面的是m

    }
    private void doFindAll(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        Map<String, Object > uno = parseRequest(req);
        System.out.println(uno);
        Map<String, Object> m = service.findAll(uno);

        printToJson(resp, m);//响应给页面的是m


    }



    }
